package yxy.game.pm2.command.union;

import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionMessage;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionLog;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟 联盟退出 */
@MessageType(proto = Command.UnionQuit, classtype = UnionMessage.UnionQuitRequest.class)
public class CMD_UnionQuit extends Command<UnionMessage.UnionQuitRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionMessage.UnionQuitRequest request) {
		String playerCode = messageSource.getTag();

		UnionMessage.UnionQuitResponse.Builder response = UnionMessage.UnionQuitResponse.newBuilder();

		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionLeader(entry);
		if (!result.equals(Result.UnionNotLeader)) {
			send(messageSource, response, result);
			return;
		}
		// 盟主不能退出
		if (result.equals(Result.UnionLeader)) {
			send(messageSource, response, result);
			return;
		}

		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		RedisLock pLock = UnionPlayer.lock(entry.getPlayerCode());
		try {
			if (uLock.tryLock() && pLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();
				UnionPlayer unionPlayer = entry.reloadUnionPlayer();

				if (union.getViceList().contains(playerCode)) {
					union.getViceList().remove(playerCode);
				}
				if (union.getMemberList().contains(playerCode)) {
					union.getMemberList().remove(playerCode);
				}
				union.set();
				union.active();

				unionPlayer.setUnionCode(null);
				unionPlayer.forbiden();
				unionPlayer.set();

				PlayTypeHandle.saveUnion(playerCode, PlayTypeModule.get(playerCode));
				new BattleHandle().powerUpdate(playerCode);
				new UnionHandle().powerUpdate(union);

				send(messageSource, response, Result.Success);

				// 联盟日志
				PlayerBean player = PlayerBean.get(unionPlayer.getPlayerCode());
				UnionLog.add(union.getCode(), GameString.联盟日志退盟(player.getName()).getText());

				// 联盟状态推送
				new UnionHandle().unionPlayerNotification(playerCode);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			pLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionMessage.UnionQuitResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}